﻿using Com.Lancetinc.Evs.Config;
using Com.Lancetinc.Evs.Reports;
using Com.Lancetinc.Evs.Windows;
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using Com.Lancetinc.Evs.Events;
using Com.Lancetinc.Evs.Helpers;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 报告模板设置
    /// </summary>
    public partial class ReportTemplateSettingUserControl : ISettingControl
    {
        private readonly ReportConfig _rptConfig = AppConfigs.ReportConfig;
        private bool UsbBanner => RdbUseBanner.IsChecked == true;

        public ReportTemplateSettingUserControl()
        {
            InitializeComponent();
            this.Loaded += ReportTemplateSettingUserControl_Loaded;
        }

        private void ReportTemplateSettingUserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            TxtTitle.Text = _rptConfig.ReportTitle;
            TxtSubTitle.Text = _rptConfig.ReportSubTitle;
            RdbUseBanner.IsChecked = _rptConfig.UseBanner == true;
            RdbUseTitle.IsChecked = !RdbUseBanner.IsChecked;
        }

        private void BtnClose_OnClick(object sender, System.Windows.RoutedEventArgs e)
        {
            RequestClose?.Invoke(this, EventArgs.Empty);
        }

        private void BtnSave_OnClick(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!ValidInput())
                return;

            RequestSave?.Invoke(this, EventArgs.Empty);
        }

        /// <inheritdoc />
        public bool CheckChanged()
        {
            if (_bannerChanged)
            {
                return true;
            }

            var title = _rptConfig.ReportTitle;
            if (title != TxtTitle.Text)
            {
                return true;
            }

            var subTitle = _rptConfig.ReportSubTitle;
            if (subTitle != TxtSubTitle.Text)
            {
                return true;
            }

            if (UsbBanner != _rptConfig.UseBanner)
            {
                return true;
            }
            return false;
        }

        /// <inheritdoc />
        public bool Save()
        {
            _rptConfig.ReportTitle = TxtTitle.Text.TrimEnd();
            _rptConfig.ReportSubTitle = TxtSubTitle.Text.TrimEnd();
            if (_selectedBanner != null && File.Exists(_selectedBanner))
            {
                _rptConfig.BannerFileName = ReportHelper.SaveBanner(_selectedBanner);
            }

            _rptConfig.UseBanner = UsbBanner;
            _rptConfig.Save();
            _selectedBanner = null;
            _bannerChanged = false;

            KnownAppEvents.PublishReportTemplatePreviewContentChanged();
            return true;
        }

        /// <inheritdoc />
        public event EventHandler RequestClose;

        /// <inheritdoc />
        public event EventHandler RequestSave;

        private string _selectedBanner;
        private bool _bannerChanged = false;
        private void BtnSelectBanner_OnClick(object sender, RoutedEventArgs e)
        {
            var ofd = new OpenFileDialog
            {
                Filter = I18nHelper.Default.GetMessage("ReportTemplateSettingUserControl.PngFilter")
            };
            if (ofd.ShowDialog() != true)
                return;

            _selectedBanner = ofd.FileName;
            _bannerChanged = true;
        }

        private void RdbUseTitle_OnChecked(object sender, RoutedEventArgs e)
        {
            WpfHelper.ExecLater(() =>
            {
                TxtTitle.Focus();
            }, 33);
        }

        private bool ValidInput()
        {
            if (!string.IsNullOrWhiteSpace(TxtSubTitle.Text) &&
                string.IsNullOrWhiteSpace(TxtTitle.Text))
            {
                // 请输入医院标题
                var msg = I18nHelper.Default.GetMessageWithArgs("PlzInput0", I18nHelper.Default.GetMessage("ReportTemplateSettingUserControl.ReportTitle"));
                MessageHelper.ShowError(msg);
                return false;
            }

            // 检查医院图标
            if (UsbBanner)
            {
                bool isBannerValid = true;
                if (_selectedBanner == null)
                {
                    var existsBannerFile = ReportHelper.GetBannerFullPath();
                    if (!File.Exists(existsBannerFile))
                    {
                        isBannerValid = false;
                    }
                }
                else if (!File.Exists(_selectedBanner))
                {
                    isBannerValid = false;
                }

                if (!isBannerValid)
                {
                    // 请选择医院图标
                    var msg = I18nHelper.Default.GetMessageWithArgs("PlzInput1", I18nHelper.Default.GetMessage("ReportTemplateSettingUserControl.HospitalIcon"));
                    MessageHelper.ShowError(msg);
                    return false;
                }
            }

            return true;
        }
    }
}